<template>
  <div id="recruiterLoginView">
    <div class="login-container">
      <div class="login-content">
        <div class="login-header">
          <div class="logo-wrapper">
            <div class="logo-glow"></div>
            <h1 class="logo-text">面试通 · 招聘者</h1>
          </div>
          <p class="subtitle">企业招聘者登录入口</p>
        </div>

        <a-form
          :model="formState"
          name="basic"
          autocomplete="off"
          @finish="onFinish"
          layout="vertical"
          class="login-form"
        >
          <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
            <a-input
              v-model:value="formState.userAccount"
              size="large"
              placeholder="请输入账号"
              :prefix="h(UserOutlined)"
            />
          </a-form-item>

          <a-form-item name="userPassword" :rules="[{ required: true, message: '请输入密码' }]">
            <a-input-password
              v-model:value="formState.userPassword"
              size="large"
              placeholder="请输入密码"
              :prefix="h(LockOutlined)"
            />
          </a-form-item>

          <a-form-item>
            <a-button
              class="login-button"
              type="primary"
              html-type="submit"
              size="large"
              :loading="loading"
            >
              招聘者登录
            </a-button>
          </a-form-item>
        </a-form>

        <div class="login-footer">
          <p>
            <a @click="router.push('/user/login')" class="user-link">用户登录</a> |
            <a @click="router.push('/admin/login')" class="admin-link">管理员登录</a> |
            <a @click="router.push('/recruiter/register')" class="register-link">招聘者注册</a>
          </p>
        </div>
      </div>
    </div>

    <!-- 装饰元素 -->
    <div class="decoration-elements">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
      <div class="dots dots-1"></div>
      <div class="dots dots-2"></div>
      <div class="wave wave-1"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, h, onMounted, nextTick } from 'vue'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import gsap from 'gsap'
import { useLoginUserStore } from '@/stores/useLoginUserstore'
import { userLoginUsingPost } from '@/api/userController'
import ACCESS_ENUM from '@/access/accessEnum'

const userStore = useLoginUserStore()
const router = useRouter()
const route = useRoute()
const loading = ref(false)

const formState = ref({
  userAccount: '',
  userPassword: '',
  role: ACCESS_ENUM.RECRUITER, // 设置为招聘者角色
})

const onFinish = async () => {
  loading.value = true

  try {
    //发送请求登录
    const data = await userLoginUsingPost({
      ...formState.value,
      role: ACCESS_ENUM.RECRUITER, // 确保以招聘者身份登录
    })
    if (data?.code === 0) {
      //登录成功
      //获取用户信息
      await userStore.fetchLoginUser()
      message.success('招聘者登录成功')
      //跳转到招聘者控制台
      router.replace({
        path: '/recruiter/dashboard',
      })
    } else {
      //登录失败
      message.error(data.message || '账号或密码错误')
    }
  } catch (error) {
    console.error('登录失败:', error)
    message.error('登录失败，请稍后再试')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  nextTick(() => {
    gsap.from('.login-container', { opacity: 0, y: 40, duration: 1, ease: 'power3.out' })
    gsap.from('.logo-wrapper', {
      opacity: 0,
      scale: 0.9,
      duration: 0.8,
      delay: 0.2,
      ease: 'back.out(1.7)',
    })
    gsap.from('.login-header .subtitle', { opacity: 0, y: 20, duration: 0.7, delay: 0.3 })
    gsap.from('.login-form .a-form-item', {
      opacity: 0,
      y: 20,
      duration: 0.7,
      delay: 0.4,
      stagger: 0.1,
    })
    gsap.from('.login-footer', { opacity: 0, y: 10, duration: 0.7, delay: 0.8 })
    gsap.from('.decoration-elements .circle', {
      opacity: 0,
      scale: 0.8,
      duration: 1,
      delay: 0.3,
      stagger: 0.2,
      ease: 'elastic.out(1, 0.5)',
    })
    gsap.from('.decoration-elements .dots', {
      opacity: 0,
      y: 10,
      duration: 1,
      delay: 0.5,
      stagger: 0.2,
    })
    gsap.from('.decoration-elements .wave', { opacity: 0, x: -10, duration: 1, delay: 0.7 })
  })
})
</script>

<style scoped>
.ant-input-password {
  height: 65px !important;
}

#recruiterLoginView {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  position: relative;
  background: linear-gradient(135deg, #f0fff4 0%, #e6ffec 100%);
  background-image:
    radial-gradient(circle at 10% 20%, rgba(0, 168, 68, 0.1) 0%, transparent 20%),
    radial-gradient(circle at 90% 80%, rgba(0, 77, 21, 0.1) 0%, transparent 20%),
    linear-gradient(to right, rgba(0, 168, 68, 0.05) 0%, rgba(0, 77, 21, 0.03) 20%, transparent 80%);
  animation: waveAnimation 15s ease infinite;
}

@keyframes waveAnimation {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.login-container {
  width: 500px;
  max-width: 90%;
  z-index: 10;
  transform: scale(1.05);
  transition: transform 0.3s ease;
}

.login-container:hover {
  transform: scale(1.08);
  box-shadow: 0 20px 50px rgba(0, 77, 21, 0.15);
}

.login-content {
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(12px);
  border-radius: 24px;
  padding: 48px;
  box-shadow:
    0 16px 40px rgba(0, 77, 21, 0.08),
    0 8px 24px rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.9);
  overflow: hidden;
  position: relative;
}

.login-content::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 6px;
  background: linear-gradient(90deg, rgba(0, 77, 21, 0.8), rgba(0, 168, 68, 0.8));
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-wrapper {
  position: relative;
  display: inline-block;
  margin-bottom: 10px;
}

.logo-text {
  font-size: 32px;
  font-weight: 700;
  margin: 0;
  background: linear-gradient(135deg, rgba(0, 77, 21, 1), rgba(0, 168, 68, 1));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 1px;
}

.logo-glow {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(0, 77, 21, 0.2) 0%, rgba(255, 255, 255, 0) 70%);
  filter: blur(15px);
  opacity: 0.8;
  z-index: -1;
  transform: scale(1.5);
}

.subtitle {
  font-size: 16px;
  color: #666;
  margin-top: 8px;
  margin-bottom: 0;
}

.login-form {
  margin-top: 40px;
}

:deep(.ant-input),
:deep(.ant-input-password) {
  height: 50px;
  border-radius: 10px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

:deep(.ant-input:hover),
:deep(.ant-input-password:hover) {
  border-color: rgba(0, 77, 21, 0.4);
}

:deep(.ant-input:focus),
:deep(.ant-input-affix-wrapper-focused) {
  border-color: rgba(0, 77, 21, 0.6) !important;
  box-shadow: 0 0 0 2px rgba(0, 77, 21, 0.1) !important;
}

:deep(.ant-input-prefix) {
  color: rgba(0, 77, 21, 0.8);
  font-size: 18px;
  margin-right: 10px;
}

.login-button {
  width: 100%;
  height: 50px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, rgba(0, 77, 21, 0.95), rgba(0, 168, 68, 0.95));
  border: none;
  box-shadow: 0 4px 12px rgba(0, 77, 21, 0.25);
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(0, 77, 21, 0.3);
  background: linear-gradient(135deg, rgb(0, 77, 21), rgb(0, 168, 68));
}

.login-button:active {
  transform: translateY(0);
  box-shadow: 0 4px 8px rgba(0, 77, 21, 0.2);
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  color: #666;
  font-size: 14px;
}

.user-link,
.admin-link,
.register-link {
  color: rgba(0, 77, 21, 0.9);
  font-weight: 500;
  transition: all 0.3s ease;
  margin: 0 5px;
}

.user-link:hover,
.admin-link:hover,
.register-link:hover {
  color: rgba(0, 77, 21, 1);
  text-decoration: underline;
}

/* 装饰元素 */
.decoration-elements {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 1;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, rgba(0, 77, 21, 0.08), rgba(0, 168, 68, 0.08));
  opacity: 0.6;
  transition:
    transform 0.8s ease,
    opacity 0.8s ease;
}

.circle-1 {
  width: 400px;
  height: 400px;
  top: -150px;
  right: -150px;
}

.circle-2 {
  width: 300px;
  height: 300px;
  bottom: -100px;
  left: -100px;
}

.circle-3 {
  width: 200px;
  height: 200px;
  top: 50%;
  left: 60%;
  opacity: 0.3;
}

.dots {
  position: absolute;
  background-image: radial-gradient(rgba(0, 77, 21, 0.2) 2px, transparent 2px);
  background-size: 20px 20px;
}

.dots-1 {
  width: 250px;
  height: 250px;
  top: 10%;
  left: 10%;
}

.dots-2 {
  width: 200px;
  height: 200px;
  bottom: 10%;
  right: 15%;
  opacity: 0.5;
  background-size: 15px 15px;
}

.wave {
  position: absolute;
  background: linear-gradient(
    -45deg,
    rgba(0, 77, 21, 0.06) 25%,
    transparent 25%,
    transparent 50%,
    rgba(0, 77, 21, 0.06) 50%,
    rgba(0, 77, 21, 0.06) 75%,
    transparent 75%,
    transparent
  );
  background-size: 20px 20px;
  border-radius: 12px;
}

.wave-1 {
  width: 300px;
  height: 150px;
  bottom: 20%;
  right: 20%;
  transform: rotate(15deg);
  opacity: 0.5;
}
</style>
